#! /bin/bash -x

set -e

clone_url_parent=https://github.com/rpm-software-management/mock

workdir=$(basename "$clone_url_parent")
workdir=${workdir%%.git}

hook_payload=$(readlink -f "${HOOK_PAYLOAD-hook_payload}")

mkdir -p "$COPR_RESULTDIR"
resultdir=$(readlink -f "$COPR_RESULTDIR")

# clone the helper scripts when needed, and add to PATH
test -d copr-ci-tooling \
    || git clone --depth 1 https://github.com/praiskup/copr-ci-tooling.git
export PATH="$PWD/copr-ci-tooling:$PATH"

# clone the tested project
git clone \
    --recursive \
    --no-single-branch \
    "$clone_url_parent"

# checkout requested revision
cd "$workdir"

copr-travis-checkout "$hook_payload"

commit=$(git rev-parse HEAD)
short_commit=${commit::5}
date=$(date +"%Y%m%d_%H%M%S")


case $COPR_RESULTDIR in
    *mock-core-configs) cd mock-core-configs ;;
    *)                  cd mock ;;
esac

tito build --srpm --test -o "$resultdir"

cd "$resultdir"
bsdtar -xf *.src.rpm
